using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatabaseCode.Data;

namespace DatabaseCode.Maker
{
  class DataioMaker
  {
    public static string OutHeader(D1_Headers headers)
    {
      string str = "";
      str += @"#ifndef COMMON_DATAIO_H_
#define COMMON_DATAIO_H_

#include ""dataio_base.h""
"; 
      foreach (D2_Header h in headers.Headers)
      {
        str += h.IncludeString();
      }
      str += @"
namespace dataio {

";
      foreach (D2_Header h in headers.Headers)
      {
        str += "class " + h.Name + ";\r\n";
      }
      str += @"
class DataIO {
public:
  DataIO();
  ~DataIO();

  //Enums::Errors Add(const Enums::Table table, const void* data, const int num);
  Enums::Errors Add(const Enums::Table table, const void* data);
  //Enums::Errors Remove(const Enums::Table table, const void* data, const int num);
  Enums::Errors Remove(const Enums::Table table, const void* data);

  Enums::Errors Update(const Enums::Table table, const void* data);

";
      foreach (D2_Header h in headers.Headers)
      {
        str += "  " + h.Name + "* " + h.Membername + "() { return " + h.Membername + "_; } const;\r\n";
      }
      str += @"
private:
";
      foreach (D2_Header h in headers.Headers)
      {
        str += "  " + h.Name + "* " + h.Membername + "_;\r\n";
      }
      str += @"};

}  // namespace dataio

#endif // COMMON_DATAIO_H_";
      return str;
    }

    public static string OutSource(D1_Headers headers)
    {
      string str = @"#include ""dataio.h""

";
      foreach (D2_Header h in headers.Headers)
      {
        str += h.IncludeString();
      }
      str += @"
namespace dataio {

DataIO::DataIO()
{
";
      foreach (D2_Header h in headers.Headers)
      {
        str += "  this->" + h.Membername + "_ = new " + h.Name + "();";
      }
      str += @"}
DataIO::~DataIO()
{";
      foreach (D2_Header h in headers.Headers)
      {
        str += "  delete this->" + h.Membername + "_;";
      }
      str += @"
}

Enums::Errors DataIO::Add(const Enums::Table table, const void* data)
{
  switch (Enums.GetWhos(table))
  {
";
      foreach(D2_Header h in headers.Headers)
      {
        str += "  case Enums::k" + h.Name + ":\r\n";
        str += "    return this->" + h.Membername + "_->Add((Enums::" + h.Name + "Table)table, data);\r\n";
      }
      str += @"
  }
}
Enums::Errors DataIO::Remove(const Enums::Table table, const void* data)
{
  switch (Enums.GetWhos(table))
  {
";
      foreach (D2_Header h in headers.Headers)
      {
        str += "  case Enums::k" + h.Name + ":\r\n";
        str += "    return this->" + h.Membername + "_->Remove((Enums::" + h.Name + "Table)table, data);\r\n";
      }
      str += @"
  }
}

Enums::Errors DataIO::Update(const Enums::Table table, const void* data)
{
  switch (Enums.GetWhos(table))
  {
";
      foreach (D2_Header h in headers.Headers)
      {
        str += "  case Enums::k" + h.Name + ":\r\n";
        str += "    return this->" + h.Membername + "_->Update((Enums::" + h.Name + "Table)table, data);\r\n";
      }
      str += @"
  }
}

}  // namespace dataio";
      return str;
    }
  }
}
